package com.ziytek.web.citizen.controller;


import com.google.gson.Gson;
import com.ziytek.web.citizen.constant.ApiConstant;
import com.ziytek.web.citizen.constant.NumConstant;
import com.ziytek.web.citizen.enums.ResponseEnum;
import com.ziytek.web.citizen.enums.StepEnum;
import com.ziytek.web.citizen.model.req.StepSettingsAddReq;
import com.ziytek.web.citizen.model.req.StepSettingsDelReq;
import com.ziytek.web.citizen.model.req.StepSettingsModifyReq;
import com.ziytek.web.citizen.model.rsp.UnifyResponse;
import com.ziytek.web.citizen.service.itf.AdminService;
import com.ziytek.web.citizen.service.itf.StepSettingService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 工步方案接口
 *
 * @author fengh
 */
@Slf4j
@RestController
@RequestMapping(ApiConstant.API_CABINET)
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class StepSettingsController {
    private final Gson gson;
    private final AdminService adminService;
    private final StepSettingService stepSettingService;

    /**
     * 工步方案新增接口
     *
     * @param request 工步方案新增请求参数(json格式)
     * @return UnifyResponse 统一响应体
     */
    @PostMapping(ApiConstant.STEP_ADD)
    public UnifyResponse stepAdd(@Valid @RequestBody StepSettingsAddReq request) {
        var response = new UnifyResponse();
        response.setRetCode(ResponseEnum.OTHER.getCode());
        response.setRetMsg(ResponseEnum.OTHER.getDesc());
        log.info("Add request parameters to the step setting scheme for the capacity divider cabinet:{}", gson.toJson(request));

        var hasPermission = adminService.hasAdminPermission(request.getToken());
        if (!hasPermission) {
            response.setRetCode(ResponseEnum.NO_PERMISSION.getCode());
            response.setRetMsg(ResponseEnum.NO_PERMISSION.getDesc());
            return response;
        }
        var code = stepSettingService.stepAdd(request, null, true);
        response.setRetCode(code);
        response.setRetMsg(StepEnum.SUCCESS.getMsg(code));
        return response;
    }

    /**
     * 工步方案修改接口
     *
     * @param request 工步方案修改请求参数(json格式)
     * @return UnifyResponse 统一响应体
     */
    @PostMapping(ApiConstant.STEP_MODIFY)
    public UnifyResponse stepModify(@Valid @RequestBody StepSettingsModifyReq request) {
        var response = new UnifyResponse();
        response.setRetCode(ResponseEnum.OTHER.getCode());
        response.setRetMsg(ResponseEnum.OTHER.getDesc());
        log.info("Modification of the Step Setting Scheme for the Capacity Distribution Cabinet:{}", gson.toJson(request));

        var hasPermission = adminService.hasAdminPermission(request.getToken());
        if (!hasPermission) {
            response.setRetCode(ResponseEnum.NO_PERMISSION.getCode());
            response.setRetMsg(ResponseEnum.NO_PERMISSION.getDesc());
            return response;
        }

        var code = stepSettingService.stepModify(request);
        response.setRetCode(code);
        response.setRetMsg(StepEnum.SUCCESS.getMsg(code));
        return response;
    }

    /**
     * 工步方案删除接口
     *
     * @param request 工步方案删除请求参数(json格式)
     * @return UnifyResponse 统一响应体
     */
    @PostMapping(ApiConstant.STEP_DEL)
    public UnifyResponse stepDel(@Valid @RequestBody StepSettingsDelReq request) {
        var response = new UnifyResponse();
        response.setRetCode(ResponseEnum.OTHER.getCode());
        response.setRetMsg("删除失败");
        log.info("Delete the step setting scheme for the capacity divider cabinet:{}", gson.toJson(request));

        var hasPermission = adminService.hasAdminPermission(request.getToken());
        if (!hasPermission) {
            response.setRetCode(ResponseEnum.NO_PERMISSION.getCode());
            response.setRetMsg(ResponseEnum.NO_PERMISSION.getDesc());
            return response;
        }

        var code = stepSettingService.stepDel(request.getId());
        if (code > NumConstant.INT_ZERO) {
            response.setRetCode(ResponseEnum.OTHER.getCode());
            response.setRetMsg("删除失败");
        }

        response.setRetCode(ResponseEnum.SUCCESS.getCode());
        response.setRetMsg(ResponseEnum.SUCCESS.getDesc());
        return response;
    }

}
